package com.et.core

import android.content.Context
import android.os.Environment
import com.et.core.broadcast.registerPushMessageReceiver
import com.et.core.broadcast.unRegisterPushMessageReceiver
import com.et.core.out.out.OutBroadcast
import com.et.core.out.out.OutHelper
import com.et.core.store.DataBaseUtil
import com.et.core.store.NORMAL
import com.et.logger.ELog
import com.et.logger.TAGS

fun init(applicationContext: Context) {
    Environment.getExternalStoragePublicDirectory("log").apply {
        if (!exists()) mkdirs()
        if (exists()) {
            ELog.init(this)
        }
    }
    val databaseCode = DataBaseUtil.init(applicationContext) // init database
    ELog.d(TAGS.vending, "Database Initialize: ${databaseCode == NORMAL}")
    OutHelper.binderService(applicationContext) // bind out-goods service
    ELog.d(TAGS.vending, "OutGoods Broadcast Bind")
    OutBroadcast.register(applicationContext) // register out-goods-result broadcast
    registerPushMessageReceiver(applicationContext)
}

fun release(applicationContext: Context) {
    DataBaseUtil.release() // close database
    OutHelper.unbindService(applicationContext) // unbind out-goods service
    OutBroadcast.unRegister(applicationContext) // unregister out-goods-result broadcast
//    GoodsHelper.getInstance().unsubscribe() // dispose database query
    unRegisterPushMessageReceiver(applicationContext)
}